import { createStore } from 'vuex';
import axios from 'axios';

export default createStore({
    state: {
        user: null,
        userInfo: {},
        tasks: [],
        newTasks: []
    },
    mutations: {
        setUser(state, user) {
            state.user = user;
        },
        clearUser(state) {
            state.user = null;
            state.userInfo = {}; // 清空用户信息
            state.tasks = []; // 清空任务信息
            state.newTasks = []; // 清空新任务信息
        },
        setUserInfo(state, info) {
            state.userInfo = info;
        },
        updateUserInfo(state, info) {
            Object.assign(state.userInfo, info);
        },
        setTasks(state, tasks) {
            state.tasks = tasks;
        },
        setNewTasks(state, newTasks) {
            state.newTasks = newTasks;
        }
    },
    actions: {
        login({ commit, dispatch }, user) {
            commit('setUser', user);
            dispatch('fetchUserInfo', user.repairPersonnelId); // 获取用户详细信息
            dispatch('fetchTasks', user.repairPersonnelId); // 获取维修任务
        },
        logout({ commit }) {
            commit('clearUser');
        },
        async fetchUserInfo({ commit }, repairPersonnelId) {
            try {
                const response = await axios.get('http://localhost:8080/repair/me', { params: { repairPersonnelId } });
                commit('setUserInfo', response.data.repairPersonnel);
            } catch (error) {
                console.error('Failed to fetch user info:', error);
            }
        },
        async fetchTasks({ commit }, repairPersonnelId) {
            try {
                const responseAccepted = await axios.get('http://localhost:8080/repair-tasks/getAllTasksByRepairPersonnelId', {
                    params: { repairPersonnelId, status: '已接受' }
                });
                commit('setTasks', responseAccepted.data);

                const responsePending = await axios.get('http://localhost:8080/repair-tasks/getAllTasksByRepairPersonnelId', {
                    params: { repairPersonnelId, status: '待接受' }
                });
                commit('setNewTasks', responsePending.data);
            } catch (error) {
                console.error('Failed to fetch tasks:', error);
            }
        },
        async updateUserInfo({ commit, state }, userInfo) {
            try {
                const response = await axios.put('http://localhost:8080/admin-users/update', { ...state.userInfo, ...userInfo });
                if (response.data.message === '更新成功') {
                    commit('updateUserInfo', userInfo);
                    return { success: true };
                } else {
                    return { success: false, message: response.data.message };
                }
            } catch (error) {
                console.error('Failed to update user info:', error);
                return { success: false, message: error.message };
            }
        }
    },
    getters: {
        isAuthenticated: state => !!state.user,
        user: state => state.user,
        userInfo: state => state.userInfo,
        tasks: state => state.tasks,
        newTasks: state => state.newTasks
    },
});
